﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.cms
{
    public partial class User_List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(User_List));
        }

        //得到总记录数
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public int GetRecordCount(string currentwhere)
        {
            return UserInfoBll.GetUserInfoCount(currentwhere, CommonConst.DB_Name);
        }

        //装载商品表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string BindTable(string jsonData)
        {
            Dictionary<string, object> DictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            DataTable dt = UserInfoBll.GetUserInfoTable( "", "", CommonConst.DB_Name);
            return  JsonHelper.DataTableToJSON(dt);
        }

        //保存商品信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string SaveUserInfo(string jsonData)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            Dictionary<string, object> DictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            try
            {
                if (DictList["cmd"].ToString() == "Add")
                {
                    if (UserInfoBll.IsExistUser(DictList["user_account"].ToString(), "0", CommonConst.DB_Name) > 0)
                    {
                        jsonResult.Add("msg", "该帐号已存在，不能添加！");
                        jsonResult.Add("isreload", false);
                    }
                    else
                    {
                        int User_Id = UserInfoBll.InsertUserInfo(DictList, CommonConst.DB_Name);
                        jsonResult.Add("user_id", User_Id.ToString());
                        jsonResult.Add("msg", "保存成功！");
                        jsonResult.Add("isreload", true);
                    }
                }
                else
                {
                    if (UserInfoBll.IsExistUser(DictList["user_account"].ToString(), DictList["user_id"].ToString(), CommonConst.DB_Name) > 0)
                    {
                        jsonResult.Add("msg", "该帐号已存在，不能修改！");
                        jsonResult.Add("isreload", false);
                    }
                    else
                    {
                        UserInfoBll.UpdateUserInfo(DictList, CommonConst.DB_Name);
                        jsonResult.Add("msg", "保存成功！");
                        jsonResult.Add("isreload", true);
                    }
                }
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //设置用户信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string UpdateUserStatus(int User_Id, int User_Status)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                int ii = UserInfoBll.UpdateUserStatus(User_Id.ToString(), User_Status.ToString(), CommonConst.DB_Name);
                if (ii == 0)
                {
                    jsonResult.Add("msg", "设置不成功！");
                    jsonResult.Add("isreload", false);
                }
                else
                {

                    var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                    Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                    dicHandleLog.Add("user_id", userInfo.Id);
                    dicHandleLog.Add("computername", userInfo.Computername);
                    dicHandleLog.Add("computerip", userInfo.Computerip);
                    dicHandleLog.Add("handle_note",((User_Status==0)?"禁用用户": "启用用户"));
                    dicHandleLog.Add("obj_guid", "User_Id=" + User_Id);
                    LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                    jsonResult.Add("msg", "设置成功！");
                    jsonResult.Add("isreload", true);
                }
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //删除用户信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string DeleteUserInfo(int User_Id)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                int ii = UserInfoBll.DeleteUserInfo(User_Id.ToString(), CommonConst.DB_Name);
                if (ii == 0)
                {
                    jsonResult.Add("msg", "该用户已在使用中，不能删除！");
                    jsonResult.Add("isreload", false);
                }
                else
                {
                    var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                    Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                    dicHandleLog.Add("user_id", userInfo.Id);
                    dicHandleLog.Add("computername", userInfo.Computername);
                    dicHandleLog.Add("computerip", userInfo.Computerip);
                    dicHandleLog.Add("handle_note", "删除用户");
                    dicHandleLog.Add("obj_guid", "User_Id=" + User_Id);
                    LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                    jsonResult.Add("msg", "删除成功！");
                    jsonResult.Add("isreload", true);
                }
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //修改用户密码
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string UpdateUserPassword(string User_Id, string User_Password)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                UserInfoBll.UpdateUserPassword(User_Id,User_Password, CommonConst.DB_Name);
                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", "修改用户密码");
                dicHandleLog.Add("obj_guid", "User_Id=" + User_Id);
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                jsonResult.Add("msg", "修改成功！");
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
